using System.Collections.Generic;
using System.Web.UI.WebControls;
using ValidationFramework.Reflection;

namespace ValidationFramework.Web.ASP
{
    public class LengthStringRuleClientValidationGenerator : IClientValidationGenerator
    {
        
        /// <inheritdoc />
        public IList<BaseValidator> CreateWebClientValidators(Rule rule, InfoDescriptor infoDescriptor)
        {
            var lengthStringRule = (LengthStringRule) rule;
            var webValidator = new RegularExpressionWebValidatorEx();
            var format = string.Format("(.|\n|\r){{{0},{1}}}", lengthStringRule.Minimum, lengthStringRule.Maximum);
            webValidator.ValidationExpression = format;
            var list = new BaseValidator[1];
            list[0] = webValidator;
            return list;
        }

        /// <inheritdoc />
        public bool CanHandleTarget(RuleContext ruleContext)
        {
            return ruleContext.Rule is LengthStringRule;
        }
    }
}